package com.boe.dao.impl;

import com.boe.dao.CategoryDao;
import com.boe.domain.Category;
import com.boe.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class CategoryImpl implements CategoryDao {

    @Override
    public List<Category> findAll() {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<Category> list = null;


        try {
            //1.获得连接
            conn = JDBCUtils.getConnection();
            //2.编写SQL
            String sql = "select * from category";
            //3.预编译SQL
            pstmt = conn.prepareStatement(sql);
            //4.设置参数
            //5.执行SQL
            rs = pstmt.executeQuery();
            //6.结果处理
            list = new ArrayList<Category>();
            while(rs.next()){
                Category category = new Category();
                category.setCid(rs.getInt("cid"));
                category.setCname(rs.getString("cname"));
                category.setCdesc(rs.getString("cdes"));
                list.add(category);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //7.资源释放
            JDBCUtils.release(rs, pstmt, conn);
        }
        return list;
    }


    //分类信息新增保存
    @Override
    public void save(Category category) {
        String cname = category.getCname();
        String cdesc = category.getCdesc();

        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            conn = JDBCUtils.getConnection();
            String sql = "insert into category values(null,?,?)";
            pstmt = conn.prepareStatement(sql);

            pstmt.setString(1,cname);
            pstmt.setString(2,cdesc);
            pstmt.executeUpdate();

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.release(rs,pstmt,conn);
        }


    }

    @Override
    public Category findOne(Integer cid) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            conn = JDBCUtils.getConnection();

            String sql = "select * from Category where cid = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,cid);
            rs = pstmt.executeQuery();
            if(rs.next()){
                Category category = new Category();
                category.setCid(rs.getInt("cid"));
                category.setCname(rs.getString("cname"));
                category.setCdesc(rs.getString("cdes"));

                return category;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.release(rs,pstmt,conn);
        }

        return null;
    }

    @Override
    public void update(Category category) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            conn = JDBCUtils.getConnection();
            String sql = "update category set cname = ?,cdes = ? where cid = ?";
            pstmt = conn.prepareStatement(sql);

            pstmt.setString(1,category.getCname());
            pstmt.setString(2,category.getCdesc());
            pstmt.setInt(3,category.getCid());

            pstmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.release(rs,pstmt,conn);
        }
    }

    @Override
    public void delete(Integer cid) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            conn = JDBCUtils.getConnection();
            String sql = "delete  from category  where cid = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,cid);
            pstmt.executeUpdate();

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.release(rs,pstmt,conn);
        }
    }
}
